package com.zz.parkingsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zz.parkingsystem.aop.LogAnnotation;
import com.zz.parkingsystem.dto.CarMessageDTO;
import com.zz.parkingsystem.dto.CarOrderMessageDTO;
import com.zz.parkingsystem.entity.Account;
import com.zz.parkingsystem.entity.Admin;
import com.zz.parkingsystem.entity.Car;
import com.zz.parkingsystem.mapper.AccountMapper;
import com.zz.parkingsystem.mapper.CarMapper;
import com.zz.parkingsystem.service.CarService;
import com.zz.parkingsystem.vo.CarMessageVO;
import com.zz.parkingsystem.vo.CarOrderMessageVO;
import com.zz.parkingsystem.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author wangyuxiang
 */
@Service
public class CarServiceImpl implements CarService {

    @Resource
    private CarMapper carMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public PageResult queryAllCarMessage(CarMessageDTO carDTO) {


        Integer pageIndex = carDTO.getPageIndex();
        Integer pageSize = carDTO.getPageSize();
        //查询数据
        PageHelper.startPage(pageIndex, pageSize);

        List<CarMessageVO> list = carMapper.queryAllCarMessage(carDTO);
        //判断车牌颜色
        for (int i = 0; i < list.size(); i++) {
            if ("0".equals(list.get(i).getClLicenseType())) {
                list.get(i).setClLicenseType("蓝");
            }
            if ("1".equals(list.get(i).getClLicenseType())) {
                list.get(i).setClLicenseType("黄");
            }
            if ("2".equals(list.get(i).getClLicenseType())) {
                list.get(i).setClLicenseType("黑");
            }
            if ("3".equals(list.get(i).getClLicenseType())) {
                list.get(i).setClLicenseType("绿");
            }
            if ("4".equals(list.get(i).getClLicenseType())) {
                list.get(i).setClLicenseType("白");
            }
        }

        PageInfo pageInfo = new PageInfo(list);
        PageHelper.clearPage();
        //返回一个自定义分页结果
        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(pageIndex);
        pageResult.setPageSize(pageSize);
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal((int) pageInfo.getTotal());
        pageResult.setList(list);
        return pageResult;
    }


    @Override
    public PageResult queryAllCarOrderMessage(CarOrderMessageDTO carOrderMessageDTO) {

        Integer pageIndex = carOrderMessageDTO.getPageIndex();
        Integer pageSize = carOrderMessageDTO.getPageSize();
        //查询数据
        PageHelper.startPage(pageIndex, pageSize);
        List<CarOrderMessageVO> list = carMapper.queryAllCarOrderMessage(carOrderMessageDTO);
        //处理支付方式，和订单状态VO
        for (CarOrderMessageVO car : list) {
            if ( "0".equals(car.getPtType())) {
                car.setPtType("支付宝");
            } else {
                car.setPtType("微信支付");
            }
            if ("0".equals(car.getOrderStatus() )) {
                car.setOrderStatus("进行中");
            }
            if (  "1".equals(car.getOrderStatus())) {
                car.setOrderStatus("未支付");
            }
            if ( "2".equals(car.getOrderStatus())) {
                car.setOrderStatus("已支付");
            }
            if ( "3".equals(car.getOrderStatus())) {
                car.setOrderStatus("申诉中");
            }
            if ( "4".equals(car.getOrderStatus())) {
                car.setOrderStatus("已退款");
            }
        }
        //处理支付方式DTO
        if ( "支付宝".equals(carOrderMessageDTO.getPtType())) {
            carOrderMessageDTO.setPtType("0");
        } else {
            carOrderMessageDTO.setPtType("1");
        }

        PageInfo pageInfo = new PageInfo(list);
        PageHelper.clearPage();
        //返回一个自定义分页结果
        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(pageIndex);
        pageResult.setPageSize(pageSize);
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal((int) pageInfo.getTotal());
        pageResult.setList(list);
        return pageResult;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return carMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Car record) {
        return carMapper.insert(record);
    }

    @Override
    public int insertSelective(Car record) {
        return carMapper.insertSelective(record);
    }

    @Override
    public Car selectByPrimaryKey(Long id) {
        return carMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(Car record) {
        return carMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(Car record) {
        return carMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public String insertSelectiveUser(Car record) {
        List<Car> cars = carMapper.selAll();
        for (Car car:cars) {
            if(car.getCarLicensePlate().equals(record.getCarLicensePlate())){
                return "车牌已经被绑定";
            }
        }
        if(record.getCarLicensePlate().length()!=7){
            return "无效车牌";
        }
        record.setCarBindTime(new Date());
        carMapper.insertSelective(record);
        //对应账户绑定车辆数加1
        Account account = accountMapper.selectByPrimaryKey(record.getAccId().longValue());
        account.setAccCarsBound(account.getAccCarsBound()+1);
        accountMapper.updateByPrimaryKeySelective(account);
        return "车辆绑定成功";
    }

    @Override
    public Car selCarByCl_id(String cl_id) {
        return carMapper.selCarByCl_id(cl_id);
    }

    @Override
    public List<Car> selCarByUserId(Integer userId) {
        return carMapper.selCarByUserId(userId);
    }

    @Override
    public Car selCarBycarLicensePlate(String carLicensePlate) {
        return carMapper.selCarBycarLicensePlate(carLicensePlate);
    }

    @Override
    public void unBindCar(Integer carId) {
        //对应的账户车辆数减1
        Car record = carMapper.selectByPrimaryKey(carId.longValue());
        Account account = accountMapper.selectByPrimaryKey(record.getAccId().longValue());
        account.setAccCarsBound(account.getAccCarsBound()-1);
        //删车
        accountMapper.updateByPrimaryKeySelective(account);
        carMapper.deleteByPrimaryKey(carId.longValue());
    }


}
